﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using Caliburn.Micro;
using Wulong.Phone.Dianping.Constants;
using Wulong.Phone.Dianping.Helpers;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Services;

namespace Wulong.Phone.Dianping.ViewModels
{
    public class PromotionListViewModel:PaginationViewModelBase
    {
        private readonly IWindowManager _windowManager;
        private IDisposable _disposable;
        private readonly INavigationService _navigation;
        public PromotionListViewModel(
            IWindowManager windowManager,
            INavigationService navigation)
        {
            _windowManager = windowManager;
            _promotions = new ObservableCollection<Promotion>();
            _navigation = navigation;
            if (Page < 1)
            {
                Page = 1;
                CanPrevPage = false;
            }
        }

        private ObservableCollection<Promotion> _promotions;
        public IEnumerable<Promotion> Promotions
        {
            get { return _promotions; }
        }
        private bool _canLoad;
        public bool CanLoad
        {
            get { return _canLoad; }
            set
            {
                _canLoad = value;
                NotifyOfPropertyChange(() => CanLoad);
            }
        }
        public void Load()
        {
            IsBusy = true;
            CanPrevPage = false;
            CanNextPage = false;
            CanLoad = false;
            Page = 1;
            _promotions.Clear();
            _disposable = DianpingService.ObservablePromotion(CityCode)
                .ObserveOnDispatcher()
                .Finally(()=>
                             {
                                 IsBusy = false;
                                 CanLoad = true;
                             })
                .Subscribe(item => _promotions.Add(item),
                    error=> MessageHelper.ShowErrorMessage(),
                    ()=>
                        {
                            if (_promotions.Count < CommonConstants.PromotionPageSize) return;
                            CanNextPage = true;
                        });
        }

        private bool _canNextPage;
        public bool CanNextPage
        {
            get { return _canNextPage; }
            set
            {
                _canNextPage = value;
                NotifyOfPropertyChange(()=>CanNextPage);
            }
        }
        public void NextPage()
        {
            IsBusy = true;
            CanPrevPage = false;
            CanNextPage = false;
            CanLoad = false;
            _promotions.Clear();
            _disposable = DianpingService.ObservablePromotion(
                CityCode, ++Page)
                .ObserveOnDispatcher()
                .Finally(() =>
                             {
                                 IsBusy = false;
                                 CanLoad = true;
                             })
                .Subscribe(item => _promotions.Add(item),
                    error =>
                        {
                            --Page;
                            MessageHelper.ShowErrorMessage();
                        },
                    () =>
                        {
                            CanPrevPage = true;
                            if (_promotions.Count < CommonConstants.PromotionPageSize) return;
                            CanNextPage = true;
                        });
        }

        private bool _canPrevPage;
        public bool CanPrevPage
        {
            get { return _canPrevPage; }
            set
            {
                _canPrevPage = value;
                NotifyOfPropertyChange(() => CanPrevPage);
            }
        }

        public void PrevPage()
        {
            if(Page<2) return;
            IsBusy = true;
            CanPrevPage = false;
            CanNextPage = false;
            CanLoad = false;
            _promotions.Clear();
            _disposable = DianpingService.ObservablePromotion(
                CityCode, --Page)
                .ObserveOnDispatcher()
                .Finally(() =>
                             {
                                 IsBusy = false;
                                 CanLoad = true;
                             })
                .Subscribe(item => _promotions.Add(item),
                    error =>
                    {
                        ++Page;
                        MessageHelper.ShowErrorMessage();
                    },
                    () =>
                        {
                            CanNextPage = true;
                            if (_promotions.Count < CommonConstants.PromotionPageSize) return;
                            CanPrevPage = true;
                        });
        }

        public void ViewItem(Promotion selectedItem)
        {
            if (selectedItem == null) return;
            var vm = new PromotionItemViewModel(
                CityCode, 
                selectedItem.Id,
                _navigation)
                         {
                             PromotionItem = selectedItem
                         };
            _windowManager.ShowDialog(vm);
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
            if(_disposable!=null)
                _disposable.Dispose();
        }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);
            if(_promotions.Count==0)
                Load();
        }
    }
}
